package com.bpc.cs.managed.bean;

import java.util.List;

import com.bpc.cs.managed.dvo.FactorDVO;
import com.bpc.cs.service.IFactorService;
import com.bpc.cs.util.Message;

/**
 * @author   phan_tp
 * @datetime Dec 19, 2012 : 9:37:25 AM
 */
public class FactorBean extends BaseBean {

	private static final long serialVersionUID = 6639730931392665471L;
	private IFactorService factorService;
	private List<FactorDVO> listFactor;
	private int selectedFactor;
	private String factorName;

	/**
	 * Add new factor name
	 */
	public void addFactor() {
		// Call service to execute addition info
		Message msg = factorService.addFactor(factorName);

		// Check return value from service
		if (msg.isResultBoolean()) {
			// Add factor name successfull
			// Get list factor again
			preGetListFactor();
		}

		// Set value to request to decide show message or not
		getRequest().setAttribute(msg.getMessageKey(), !msg.isResultBoolean());
	}

	/**
	 * Execute loading get list all factor before call getListFactor() method
	 */
	public void preGetListFactor() {
		listFactor = factorService.getAllFactor();
	}

	/**
	 * Get factorDVO from factorID
	 * @param int factorID
	 * @return FactorDVO
	 */
	public FactorDVO getSelectedFactor(int factorID) {
		for (FactorDVO factorDVO : listFactor) {
			if (factorDVO.getFactorID() == factorID) {
				return factorDVO;
			}
		}

		// FactorID doesn't exist in list
		return null;
	}

	/**
	 * @return the factorService
	 */
	public IFactorService getFactorService() {
		return factorService;
	}

	/**
	 * @param factorService the factorService to set
	 */
	public void setFactorService(IFactorService factorService) {
		this.factorService = factorService;
	}

	/**
	 * @return the selectedFactor
	 */
	public int getSelectedFactor() {
		return selectedFactor;
	}

	/**
	 * @param int the selectedFactor to set
	 */
	public void setSelectedFactor(int selectedFactor) {
		this.selectedFactor = selectedFactor;
	}

	/**
	 * @return the factorName
	 */
	public String getFactorName() {
		return factorName;
	}

	/**
	 * @param factorName the factorName to set
	 */
	public void setFactorName(String factorName) {
		this.factorName = factorName;
	}

	/**
	 * @return the listFactor
	 */
	public List<FactorDVO> getListFactor() {
		return listFactor;
	}

	/**
	 * @param listFactor the listFactor to set
	 */
	public void setListFactor(List<FactorDVO> listFactor) {
		this.listFactor = listFactor;
	}


}
